﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DeyuMvc.Dbs;
using DeyuMvc.Models;

namespace DeyuMvc.Controllers
{
    public class StudentController : Controller
    {
        StudentsBll bll = new StudentsBll();
        // GET: Student
        public ActionResult Index()
        {
            var list = bll.GetAll();

            return View(list);
        }

        // GET: Student/Details/5
        public ActionResult Details(string id)
        {
            var obj = bll.GetByKey(id);

            return View(obj);
        }

        // GET: Student/Create
        public ActionResult Create()
        {
            Students obj = new Students();

            obj.StuCode = Commons.GenerateIdByForeTag("STU");

            return View(obj);
        }

        // POST: Student/Create
        [HttpPost]
        public ActionResult Create(Students stu)
        {
            try
            {
                bll.Insert(stu);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Student/Edit/5
        public ActionResult Edit(string id)
        {
            var obj = bll.GetByKey(id);

            return View(obj);
        }

        // POST: Student/Edit/5
        [HttpPost]
        public ActionResult Edit(Students stu)
        {
            try
            {
                // TODO: Add update logic here
                bll.Update(stu);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Student/Delete/5
        public ActionResult Delete(string id)
        {
            var obj = bll.GetByKey(id);

            return View(obj);
        }

        // POST: Student/Delete/5
        [HttpPost]
        public ActionResult Delete(Students stu)
        {
            try
            {
                // TODO: Add delete logic here
                bll.Delete(stu);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
